/**
 * Created by x on 17-7-18.
 */

angular.module('tribalc').service(  'ApartmentInfoModel',['Api','$rootScope', 'Check' ,function (Api, $rootScope,Check){


    var ApartmentInfoModel = function (object) {
        var self = this;
        init();
        function init() {

            self.id =object? object.id :null;
            self.status =object ? object.status : null;
            self.num =object ? object.num : null;
            self.name =object ? object.name : null;
            self.price =object ? Number(object.price) : null;
            self.area =object ? Number(object.area ): null;
            self.address =object ? object.address : null;
            self.subway =object ? object.subway : null;
            self.subwayDistance =object ? object.subwayDistance : null;
            self.type =object ? object.type : null;
            self.tags =object ? object.tags : null;
            self.pictures =object ? getReallyPicturePaths(object.pictures) : null;
            self.around =object ? object.around : null;
            self.traffic =object ? object.traffic : null;
            self.facilities =object ? object.facilities : null;
            self.floor =object ? Number(object.floor) : null;
            self.buildingHeight =object ? Number(object.buildingHeight) : null;
            self.provinceId = object ? object.provinceId : null;
            self.cityId = object ? object.cityId : null;
            self.disId = object ? object.disId : null;
            self.communityId = object ? object.communityId : null;
            self.buildingId = object ? object.buildingId : null;
            self.buildingName = object ? object.buildingName : null;
        }

    };



    ApartmentInfoModel.prototype.getObject = function () {
        return {
            id:this.id,
            name: this.name,
            price: this.price,
            area: this.area,
            address: this.address,
            subway: this.subway,
            subwayDistance: this.subwayDistance,
            type: this.type,
            tags: this.tags,
            pictures: getOssPicturePaths(this.pictures),
            around: this.around,
            traffic: this.traffic,
            facilities: this.facilities,
            floor: this.floor,
            buildingHeight: this.buildingHeight,
            provinceId: this.provinceId,
            cityId: this.cityId,
            disId: this.disId,
            communityId:this.communityId,
            buildingId:this.buildingId,
            buildingName:this.buildingName
        };
    };

    function getReallyPicturePaths(pictures) {
        if (!pictures) return [];
        Array.from(pictures).forEach(function (path, index) {
            pictures[index] = ApartmentInfoModel.formatReallyPicturePath(path);
        });
        return pictures;
    }

    function getOssPicturePaths(pictures) {
        if (!pictures) return [];
        Array.from(pictures).forEach(function (path, index) {
            pictures[index] = ApartmentInfoModel.formatOssPicturePath(path);
        });
        return pictures;
    }



    ApartmentInfoModel.formatOssPicturePath = function (path) {
        if (path && path.indexOf(Api.ALIYUN_URL) !== -1) {
            path = 'oss://' + path.split(Api.ALIYUN_URL)[1];
        }
        return path;
    };

    ApartmentInfoModel.formatReallyPicturePath = function (path) {

        var oss = 'oss://';
        if (path && path.indexOf(oss) !== -1) {
            path = Api.ALIYUN_URL + path.split(oss)[1];
        }
        return path;
    };


    return ApartmentInfoModel;


}]);